//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<composition.cpp>
///	@path	~/src/gui/xgui/
///	@date	2008/05/27
///	@desc	.

#define	_FULLAPI
#include "config/config.h"

#include "gui/composite/composition.h"

namespace xeres {

	// Composition
	Composition::Composition( Layer * input )
		: m_layer( *input )
		, m_painter( NULL )
	{
		m_painter = new (m_painterBuffer) Painter( &m_layer );
	}
	// ~Composition
	Composition::~Composition( void )
	{
		m_painter->~Painter();
	}
	// BeginPaint
	bool Composition::BeginPaint( const Rect<int>& rect )
	{
		if( IsVisible( rect ) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// EndPaint
	void Composition::EndPaint( void )
	{
		m_painter->GetGraphics().Flush( Gdiplus::FlushIntentionSync );
	}
	// IsVisible
	bool Composition::IsVisible( const Rect<int>& rect )
	{
		return graphics().IsVisible( toRect(rect) ) ? true : false;
	}
	// IsVisible
	bool Composition::IsVisible( const Pair<int>& pt )
	{
		return graphics().IsVisible( Gdiplus::Point( pt.x , pt.y ) ) ? true : false;
	}
	// ClippingGuard
	ClippingGuard::ClippingGuard( Composition& composition ) : m_composition( composition )
	{
		// save clip
		m_composition.graphics().GetClip( &m_region );
	}
	// RestoreClip
	void ClippingGuard::RestoreClip( void )
	{
		m_composition.graphics().SetClip( &m_region , Gdiplus::CombineModeReplace );
	}
	// ~ClippingGuard
	ClippingGuard::~ClippingGuard( void )
	{
		// restore clip
		RestoreClip();
	}
	// TransformGuard
	TransformGuard::TransformGuard( Composition& composition ) : m_graphics( composition.graphics() )
	{
		m_graphics.GetTransform( &m_matrix );
	}
	// TransformGuard
	TransformGuard::TransformGuard( Composition& composition , float x , float y ) : m_graphics( composition.graphics() )
	{
		m_graphics.GetTransform( &m_matrix );
		m_graphics.TranslateTransform( x , y , Gdiplus::MatrixOrderAppend );
	}
	// ~TransformGuard
	TransformGuard::~TransformGuard( void )
	{
		RestoreTransform();
	}
	// RestoreTransform
	void TransformGuard::RestoreTransform( void )
	{
		// restore transform
		m_graphics.SetTransform( &m_matrix );
	}
	// ResetTransform
	void TransformGuard::ResetTransform( float x , float y )
	{
		// restore transform
		m_graphics.SetTransform( &m_matrix );
		m_graphics.TranslateTransform( x , y , Gdiplus::MatrixOrderAppend );
	}
} // namespace xeres
